package com.dimong.www.loan.widget;

import android.app.Activity;
import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

import com.dimong.loan.sdk.ui.adapter.BaseRecyclerAdapter;
import com.dimong.loan.sdk.utils.UIUtils;
import com.dimong.www.loan.R;

import java.util.List;

/**
 * 复制/回复/删除等操作的pop弹出框
 */
public class LoanSelectPopWindow extends PopupWindow {
    private ViewGroup contentView;
    private LoanSelectAdapter loanSelectAdapter;
    private View ll_ppw_loan_select;
    private RecyclerView recycler_share;
    private View tv_ppw_loan_select_cancel;
    private Activity mActivity;
    private LoanSelectCallback loanSelectCallback;

    public LoanSelectPopWindow(final Activity context) {
        mActivity = context;
        contentView = (ViewGroup) LayoutInflater.from(context).inflate(R.layout.ppw_loan_select, null);
        setContentView(contentView);
        setWidth(UIUtils.getWidth());
        setHeight(UIUtils.getHeight() - UIUtils.getActionBarHeight());

        // 设置SelectPicPopupWindow弹出窗体可点击
        setFocusable(true);
        setOutsideTouchable(true);
        // 刷新状态
        update();
        contentView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
//                if (event.getAction() == MotionEvent.ACTION_UP) {
//                    dismiss();
//                }
                return true;
            }
        });
        initView();
    }

    private void initView() {
        tv_ppw_loan_select_cancel = contentView.findViewById(R.id.tv_ppw_loan_select_cancel);
        ll_ppw_loan_select = contentView.findViewById(R.id.ll_ppw_loan_select);
        recycler_share = contentView.findViewById(R.id.recycler_share);

        loanSelectAdapter = new LoanSelectAdapter(mActivity);
        recycler_share.setLayoutManager(new LinearLayoutManager(mActivity, LinearLayoutManager.VERTICAL, false));
        recycler_share.setAdapter(loanSelectAdapter);
        recycler_share.setItemAnimator(null);
        loanSelectAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClickListener(View view, int position) {
                if (loanSelectCallback != null) {
                    loanSelectCallback.loanSelectClick(position);
                }
                dismiss();
            }
        });
        tv_ppw_loan_select_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    /**
     * 弹出操作框
     *
     * @param anchor 锚点view
     */
    public void showPopupWindow(View anchor, List<String> datas) {
        if (!isShowing()) {
            ll_ppw_loan_select.setBackgroundColor(Color.parseColor("#60000000"));
            if (datas != null) {
                loanSelectAdapter.setData(datas);
            }
            loanSelectAdapter.updateChange();
            showAtLocation(anchor, Gravity.BOTTOM, 0, 0);
        } else {
            dismiss();
        }
    }

    public void setLoanSelectCallback(LoanSelectCallback loanSelectCallback) {
        this.loanSelectCallback = loanSelectCallback;
    }

    public interface LoanSelectCallback {

        void loanSelectClick(int viewPosition);
    }
}
